/*
 * =====================================================================================
 *
 *       Filename:  cl_server_socket.hxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  03/24/2011 12:39:35 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ken Chen (Ken), chen_kenz@emc.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_SERVER_SOCKET_HXX_
#define _CL_SERVER_SOCKET_HXX_

#include <memory>
#include "cl_socket_addr.hxx"
#include "cl_socket.hxx"

namespace neo
{

class CLChannel;
class CLEventLooper;
class CLServerSocket: public CLSocket
{
private:
  const static int CL_LISTEN_QSIZE = 64;

public:
  typedef std::function<void (int connfd, CLSocketAddrUPtr peerAddr)> NewConnCallback;

public:
  CLServerSocket(CLEventLooper* looper,
                 const std::string& host, 
                 int port, 
                 CLSocketType type);
  ~CLServerSocket();
  bool listen(int listenBacklogSize = CL_LISTEN_QSIZE);
  bool listening(void) const;
  void accept(void);
  void setNewConnCallback(const NewConnCallback& cb);

private:
  bool bind(void);

private:
  bool m_listening;
  // CLServerSocket does not own m_eventLooper's lifeline.
  CLEventLooper* m_eventLooper;
  std::unique_ptr<CLChannel> m_acceptChannel;
  NewConnCallback m_connCallback;
};

} // neo 

#endif //_CL_SERVER_SOCKET_HXX_

